home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 6 / MacMania 6.toast / / Tools&Utilities / EnterAct Stuff / Indent project / Indent Source / backup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-24  |  9.5 KB  |  372 lines  |  [TEXT/MPS ]

  1. /* Copyright (c) 1993,1994, Joseph Arceneaux.  All rights reserved.
  2.  
  3.    This file is subject to the terms of the GNU General Public License as
  4.    published by the Free Software Foundation.  A copy of this license is
  5.    included with this software distribution in the file COPYING.  If you
  6.    do not have a copy, you may obtain a copy by writing to the Free
  7.    Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  8.  
  9.    This software is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details. */
  13.  
  14.  
  15.  
  16. /* GNU/Emacs style backups --
  17.    This behaviour is controlled by two environment variables,
  18.    VERSION_CONTROL and SIMPLE_BACKUP_SUFFIX.
  19.  
  20.    VERSION_CONTROL determines what kinds of backups are made.  If it's
  21.    value is "numbered", then the first modification of some file
  22.    "eraserhead.c" will yield a backup file "eraserhead.c.~1~", the
  23.    second modification will yield "eraserhead.c.~2~", and so on.  It
  24.    does not matter if the version numbers are not a sequence;  the next
  25.    version will be one greater than the highest in that directory.
  26.  
  27.    If the value of VERSION_CONTROL is "numbered_existing", then such
  28.    numbered backups will be made if there are already numbered backup
  29.    versions of the file.  Otherwise, the backup name will be that of
  30.    the original file with "~" (tilde) appended.  E.g., "eraserhead.c~".
  31.  
  32.    If the value of VERSION_CONTROL is "simple", then the backup name
  33.    will be that of the original file with "~" appended, regardless of
  34.    whether or not there exist numbered versions in the directory.
  35.  
  36.    For simple backups, the value of SIMPLE_BACKUP_SUFFIX will be used
  37.    rather than "~" if it is set.
  38.  
  39.    If VERSION_CONTROL is unset, "numbered_existing" is assumed.  For
  40.    Emacs lovers, "nil" is equivalent to "numbered_existing" and "t" is
  41.    equivalent to "numbered".
  42.  
  43.    Finally, if VERSION_CONTROL is "none" or "never", backups are not
  44.    made.  I suggest you avoid this behaviour. */
  45.  
  46. /* Written by jla, based on code from djm (see `patch') */
  47.  
  48. #include <string.h>
  49. #include <fcntl.h> // for creat
  50. #include <unistd.h> // for write and close
  51.  
  52. #include "sys.h"
  53. #include "backup.h"
  54. #include <ctype.h>
  55.  
  56. #ifndef isascii
  57. #define ISDIGIT(c) (isdigit ((unsigned char) (c)))
  58. #else
  59. #define ISDIGIT(c) (isascii (c) && isdigit (c))
  60. #endif
  61.  
  62. #ifndef NODIR
  63.  
  64. #include <sys/types.h>
  65.  
  66. #ifdef DIRENT
  67. #include <dirent.h>
  68. #ifdef direct
  69. #undef direct
  70. #endif
  71. #define direct dirent
  72. #define NLENGTH(direct) (strlen((direct)->d_name))
  73. #else /* !DIRENT */
  74. #define NLENGTH(direct) ((direct)->d_namlen)
  75. #ifdef USG
  76. #ifdef SYSNDIR
  77. #include <sys/ndir.h>
  78. #else /* !SYSNDIR */
  79. #include <ndir.h>
  80. #endif /* !SYSNDIR */
  81. #else /* !USG */
  82. #include <sys/dir.h>
  83. #endif /* !USG */
  84. #endif /* !DIRENT */
  85.  
  86. #if defined (HAVE_UNISTD_H)
  87. #include <unistd.h>
  88. #endif
  89.  
  90. #if defined (_POSIX_VERSION)    /* Might be defined in unistd.h.  */
  91. /* POSIX does not require that the d_ino field be present, and some
  92.    systems do not provide it. */
  93. #define REAL_DIR_ENTRY(dp) 1
  94. #else
  95. #define REAL_DIR_ENTRY(dp) ((dp)->d_ino != 0)
  96. #endif
  97.  
  98. #else /* NODIR */
  99. #define generate_backup_filename(v,f) simple_backup_name((f))
  100. #endif /* NODIR */
  101.  
  102. #ifndef BACKUP_SUFFIX_STR
  103. #define BACKUP_SUFFIX_STR    "~"
  104. #endif
  105.  
  106. #ifndef BACKUP_SUFFIX_CHAR
  107. #define BACKUP_SUFFIX_CHAR   '~'
  108. #endif
  109.  
  110. #ifndef BACKUP_SUFFIX_FORMAT
  111. #define BACKUP_SUFFIX_FORMAT "%s.~%d~"
  112. #endif
  113.  
  114.  
  115. /* Default backup file suffix to use */
  116. static char *simple_backup_suffix = BACKUP_SUFFIX_STR;
  117.  
  118. /* What kinds of backup files to make -- see
  119.    table `version_control_values' below. */
  120. enum backup_mode version_control = unknown;
  121.  
  122. // Functions defined here:
  123. static char *simple_backup_name (char *pathname);
  124. #ifndef NODIR
  125. static int version_number (char *base, char *direntry, int base_length);
  126. static int highest_version (char *filename, char *dirname);
  127. static int max_version (char *pathname);
  128. static char *generate_backup_filename (enum backup_mode version_control, char *pathname);
  129. #endif /* !NODIR */
  130. enum backup_mode version_control_value (void);
  131. void initialize_backups (void);
  132. void make_backup (struct file_buffer *file);
  133.  
  134.  
  135. /* Construct a simple backup name for PATHNAME by appending
  136.    the value of `simple_backup_suffix'. */
  137.  
  138. static char *
  139. simple_backup_name (char *pathname)
  140. {
  141.   char *backup_name;
  142.  
  143.   backup_name = xmalloc (strlen (pathname)
  144.              + strlen (simple_backup_suffix) + 2);
  145.   sprintf (backup_name, "%s%s", pathname, simple_backup_suffix);
  146.   return backup_name;
  147. }
  148.  
  149. #ifndef NODIR
  150. /* If DIRENTRY is a numbered backup version of file BASE, return
  151.    that number.  BASE_LENGTH is the string length of BASE. */
  152.  
  153. static int
  154. version_number (char *base, char *direntry, int base_length)
  155. {
  156.   int version;
  157.   char *p;
  158.  
  159.   version = 0;
  160.   if (!strncmp (base, direntry, base_length)
  161.       && ISDIGIT (direntry[base_length + 2]))
  162.     {
  163.       for (p = &direntry[base_length + 2]; ISDIGIT (*p); ++p)
  164.     version = version * 10 + *p - '0';
  165.       if (p[0] != BACKUP_SUFFIX_CHAR || p[1])
  166.     version = 0;
  167.     }
  168.  
  169.   return version;
  170. }
  171.  
  172.  
  173. /* Return the highest version of file FILENAME in directory
  174.    DIRNAME.  Return 0 if there are no numbered versions. */
  175.  
  176. static int
  177. highest_version (char *filename, char *dirname)
  178. {
  179.   DIR *dirp;
  180.   struct direct *dp;
  181.   int highest_version;
  182.   int this_version;
  183.   int file_name_length;
  184.  
  185.   dirp = opendir (dirname);
  186.   if (!dirp)
  187.     return 0;
  188.  
  189.   highest_version = 0;
  190.   file_name_length = strlen (filename);
  191.  
  192.   while ((dp = readdir (dirp)) != 0)
  193.     {
  194.       if (!REAL_DIR_ENTRY (dp) || NLENGTH (dp) <= file_name_length + 2)
  195.     continue;
  196.  
  197.       this_version = version_number (filename, dp->d_name, file_name_length);
  198.       if (this_version > highest_version)
  199.     highest_version = this_version;
  200.     }
  201.  
  202.   closedir (dirp);
  203.   return highest_version;
  204. }
  205.  
  206.  
  207. /* Return the highest version number for file PATHNAME.  If there
  208.    are no backups, or only a simple backup, return 0. */
  209.  
  210. static int
  211. max_version (char *pathname)
  212. {
  213.   register char *p;
  214.   register char *filename;
  215.   int pathlen = strlen (pathname);
  216.   int version;
  217.  
  218.   p = pathname + pathlen - 1;
  219.   while (p > pathname && *p != '/')
  220.     p--;
  221.  
  222.   if (*p == '/')
  223.     {
  224.       int dirlen = p - pathname;
  225.       register char *dirname;
  226.  
  227.       filename = p + 1;
  228.       dirname = xmalloc (dirlen + 1);
  229.       strncpy (dirname, pathname, (dirlen));
  230.       dirname[dirlen] = '\0';
  231.       version = highest_version (filename, dirname);
  232.       free(dirname);
  233.       return version;
  234.     }
  235.  
  236.   filename = pathname;
  237.   version = highest_version (filename, ".");
  238.   return version;
  239. }
  240.  
  241.  
  242. /* Generate a backup filename for PATHNAME, dependent on the
  243.    value of VERSION_CONTROL. */
  244.  
  245. static char *
  246. generate_backup_filename (enum backup_mode version_control, char *pathname)
  247. {
  248.   int last_numbered_version;
  249.   char *backup_name;
  250.  
  251.   if (version_control == none)
  252.     return 0;
  253.  
  254.   if (version_control == simple)
  255.     return simple_backup_name (pathname);
  256.  
  257.   last_numbered_version = max_version (pathname);
  258.   if (version_control == numbered_existing
  259.       && last_numbered_version == 0)
  260.     return simple_backup_name (pathname);
  261.  
  262.   last_numbered_version++;
  263.   backup_name = xmalloc (strlen (pathname) + 16);
  264.   if (!backup_name)
  265.     return 0;
  266.  
  267.   sprintf (backup_name, BACKUP_SUFFIX_FORMAT, pathname,
  268.        (int) last_numbered_version);
  269.   return backup_name;
  270. }
  271.  
  272. #endif /* !NODIR */
  273.  
  274. static struct version_control_values values[] =
  275. {
  276.   {none, "never"},        /* Don't make backups. */
  277.   {simple, "simple"},        /* Only simple backups */
  278.   {numbered_existing, "existing"},    /* Numbered if they already exist */
  279.   {numbered_existing, "nil"},    /* Ditto */
  280.   {numbered, "numbered"},    /* Numbered backups */
  281.   {numbered, "t"},        /* Ditto */
  282.   {unknown, 0}            /* Initial, undefined value. */
  283. };
  284.  
  285.  
  286. //extern char *getenv (char *);
  287.  
  288. /* Determine the value of `version_control' by looking in the
  289.    environment variable "VERSION_CONTROL".  Defaults to
  290.    numbered_existing. */
  291.  
  292. enum backup_mode
  293. version_control_value ()
  294. {
  295.   char *version;
  296.   struct version_control_values *v;
  297.  
  298.   version = getenv ("VERSION_CONTROL");
  299.   if (version == 0 || *version == 0)
  300.     return numbered_existing;
  301.  
  302.   v = &values[0];
  303.   while (v->name)
  304.     {
  305.       if (strcmp (version, v->name) == 0)
  306.     return v->value;
  307.       v++;
  308.     }
  309.  
  310.   return unknown;
  311. }
  312.  
  313.  
  314. /* Initialize information used in determining backup filenames. */
  315.  
  316. void
  317. initialize_backups ()
  318. {
  319. static short sNumberOfVisits = 0;
  320.   
  321.   if (sNumberOfVisits == 0)
  322.       {
  323.       char *v = getenv ("SIMPLE_BACKUP_SUFFIX");
  324.     
  325.       if (v && *v)
  326.         simple_backup_suffix = v;
  327. #ifdef NODIR
  328.       version_control = simple;
  329. #else /* !NODIR */
  330.       version_control = version_control_value ();
  331.       if (version_control == unknown)
  332.         {
  333.           fprintf (stderr, "indent:  Strange version-control value\n");
  334.           fprintf (stderr, "indent:  Using numbered-existing\n");
  335.           version_control = numbered_existing;
  336.         }
  337. #endif /* !NODIR */
  338.     }
  339.   ++sNumberOfVisits;
  340. }
  341.  
  342. /* Prints an error message using `perror' */
  343. extern void sys_error (char *);
  344.  
  345. /* Make a backup copy of FILE, taking into account version-control.
  346.    See the description at the beginning of the file for details. */
  347.  
  348. void
  349. make_backup (struct file_buffer *file)
  350. {
  351.   int fd;
  352.   register char *p = file->name + strlen (file->name) - 1;
  353.   char *backup_filename;
  354.   char *new_backup_name;
  355.  
  356.   backup_filename = generate_backup_filename (version_control, file->name);
  357.   if (!backup_filename)
  358.     {
  359.       fprintf (stderr, "indent: Can't make backup filename of %s", file->name);
  360.       exit(1);
  361.     }
  362.  
  363.   fd = creat (backup_filename, 0666);
  364.   if (fd < 0)
  365.     sys_error (backup_filename);
  366.   if (write (fd, file->data, file->size) != file->size)
  367.     sys_error (backup_filename);
  368.  
  369.   close (fd);
  370.   free(backup_filename);
  371. }
  372.